package com.wys.mall.product.webcontroller;

import com.wys.mall.product.entity.CategoryEntity;
import com.wys.mall.product.redis.RedisTool;
import com.wys.mall.product.service.CategoryService;
import com.wys.mall.product.vo.Catelog2Vo;
import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
import java.util.Map;

/**
 * 页面跳转
 *
 * @Package com.wys.mall.product.webcontroller
 * @Author charlin
 * @Version: v1.0
 * @Date 2021-06-2610:28
 * @Copyright: 2021 www.wyscha.com Inc. All rights reserved.
 */
@Controller
public class IndexController {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private RedissonClient redissonClient;

    /**
     * 跳转到首页
     *
     * @return
     */
    @GetMapping({"/", "/index.html", "/index"})
    public String indexPage(Model model) {
        List<CategoryEntity> categories = categoryService.getOneLevelCategories();
        model.addAttribute("categories", categories);
        return "index";
    }

    @ResponseBody
    @GetMapping({"/index/getCategoryJson"})
    public Map<String, List<Catelog2Vo>> getCategoryJson(Model model) {
        Map<String, List<Catelog2Vo>> map = categoryService.getCategoryJson();
        return map;
    }


    @ResponseBody
    @GetMapping("/hello")
    public String hello(){
        //获得锁
        RLock rlock = redissonClient.getLock("rlock");
        //加锁
        rlock.lock();
        try {
            System.out.println("加锁成功了..........................");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }finally {
            System.out.println("解锁：" + Thread.currentThread().getId());
            //解锁
            rlock.unlock();
        }
        return "hello";
    }

    @ResponseBody
    @GetMapping("/writeLock")
    public String writeLock(){
        //获得锁
        RReadWriteLock rwlock = redissonClient.getReadWriteLock("rwlock");
        //加锁
        RLock rLock = rwlock.writeLock();
        try {
            new RedisTool().set("name", "tom");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }finally {
            System.out.println("解写锁：" + Thread.currentThread().getId());
            //解锁
            rLock.unlock();
        }
        return "writeLock";
    }

    @ResponseBody
    @GetMapping("/readLock")
    public String readLock(){
        //获得锁
        RReadWriteLock rwlock = redissonClient.getReadWriteLock("rwlock");
        //加锁
        RLock rLock = rwlock.readLock();
        try {
            System.out.println("加读锁成功了..........................");
            String name = new RedisTool().get("name");
            System.out.println(name);
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }finally {
            System.out.println("解读锁：" + Thread.currentThread().getId());
            //解锁
            rLock.unlock();
        }
        return "readLock";
    }

    /**
     * 闭锁
     * @return
     * @throws InterruptedException
     */
    @ResponseBody
    @GetMapping("/lockDoor")
    public String lockDoor() throws InterruptedException {
        //获得锁
        RCountDownLatch door = redissonClient.getCountDownLatch("door");
        //添加记数器
        door.trySetCount(5);
        //等待
        door.await();
        return "全部走完了...";
    }

    @ResponseBody
    @GetMapping("/go/{num}")
    public String go(@PathVariable Integer num){
        RCountDownLatch door = redissonClient.getCountDownLatch("door");
        door.countDown();//计数减一
        return num + "班的人走了...";
    }

    @ResponseBody
    @GetMapping("/park")
    public String park() throws InterruptedException {
        //获得信号量
        RSemaphore park = redissonClient.getSemaphore("park");
        //占用车位
        park.acquire();
        return "获得一个车位...";
    }

    @ResponseBody
    @GetMapping("/leave")
    public String leave(){
        RSemaphore park = redissonClient.getSemaphore("park");
        park.release();//释放一个车位
        return "释放一个车位...";
    }
}
